"""
# -*- coding: utf-8 -*-
# @Time    : 2023/5/25 10:32
# @Author  : 王摇摆
# @FileName: Data_Simple.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

from PLA.Data import createTrainDatas, buildLine
from PLA.Learning_Progress.PLA_Manual import plaReturn

# 全局设置
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 选择一个本地的支持中文的字体

# 生成数据集
start = -10
end = 10
W = np.array([5, 4])
X, y = createTrainDatas(W, start, end, size=200)

# 数据处理：数据分类
x1 = X[y > 0][:, 0]
y1 = X[y > 0][:, 1]
x2 = X[y < 0][:, 0]
y2 = X[y < 0][:, 1]

Ws, errors = plaReturn(X, y)
fig = plt.figure()
plt.title('PLA复杂数据集学习动态图', fontsize=18, fontproperties='KaiTi')

# 散点图绘制
p1 = plt.scatter(x1, y1, c='b', marker='o', s=20)
p2 = plt.scatter(x2, y2, c='r', marker='o', s=20)
line, = plt.plot(0, 0, markersize=0, label='W')
error, = plt.plot(0, 0, c='k', marker='x', markersize=0)


# 更新图线的update函数
def update(i):  # 定义一个更新函数update()，用于更新图形
    x3, y3 = buildLine(Ws[i], start, end)
    line.set_data(x3, y3)
    line.set_label("第%d次更新W" % (i + 1))
    if i < len(errors):
        error.set_markersize(14)
        error.set_data(X[errors[i]][0], X[errors[i]][1])
    else:
        error.set_markersize(0)
        error.set_data(0, 0)
    plt.legend(loc="upper right")  # 更新图例标签
    return line, error,


# 绘制动图
ani = animation.FuncAnimation(fig, update, range(0, len(Ws)), interval=1000, blit=True, repeat=False)
ani.save('pla.gif')
plt.legend(loc="upper right")
plt.show()
